﻿package org.nickro.project.rpc 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.Timer;
	
	import org.nickro.project.events.FaultEvent;
	import org.nickro.project.events.ResultEvent;
	import org.nickro.project.debug.Console;
	import org.nickro.project.debug.Debug;
	/**
	 * ...
	 * @author com.nickro
	 */
	public class RomoteObject extends EventDispatcher
	{
		public function RomoteObject()
		{
			addEventListener(ResultEvent.RESULT, onResult);
			addEventListener(FaultEvent.FAULT, onFault);
			
			var delayTimer:Timer = new Timer(10, 1);
			delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, invalidate);
			delayTimer.start();
		}
	
		private function invalidate(e:TimerEvent):void
		{
			e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, invalidate);
			
			var urlloader:URLLoader = new URLLoader();
			urlloader.dataFormat = resultFormat;
			
			if (postData) urlloader.data = postData;
			if (url != '') {
				trace("data:"+urlloader.data)
				var urlReq:URLRequest = new URLRequest(url);
				urlReq.method = method;
				urlloader.addEventListener(Event.COMPLETE, onURLLoaded);
				urlloader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
				urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
				urlloader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
				urlloader.addEventListener(ProgressEvent.PROGRESS, onURLLoading);
				urlloader.load(urlReq);
				Debug.log("debug:"+urlReq.method);
				Debug.log("urlReq:"+urlReq)
			}
		}
		
		private function onHTTPStatus(e:HTTPStatusEvent):void
		{
			Console.log('Debug [RomoteObject] :: HTTPStatus ' + e.status);
		}
		
		private function onIOError(e:IOErrorEvent):void
		{
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, e.text));
		}
		
		private function onSecurityError(e:SecurityErrorEvent):void
		{
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, e.text));
		}
		
		private function onURLLoading(e:ProgressEvent):void
		{
			
		}
		
		private function onURLLoaded(e:Event):void
		{
			e.currentTarget.removeEventListener(Event.COMPLETE, onURLLoaded);
			e.currentTarget.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
			e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			e.currentTarget.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onURLLoading);
			
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, e.target.data));
		}
		
		private function onResult(e:ResultEvent):void
		{
			if (result != null) {
				removeEventListener(ResultEvent.RESULT, onResult);
				if (fault != null) removeEventListener(FaultEvent.FAULT, onFault);
				result.apply(null, [e]);
			}
		}
		
		private function onFault(e:FaultEvent):void
		{
			if (fault != null) fault.apply(this, [e]);
		}
		//传递的参数
		private var _postData:* = null;
		public function set postData(value:*):void { _postData = value; };
		public function get postData():* { return _postData; };
		// 提交方式POST/GET
		private var _method:String = 'POST';
		public function set method(value:String):void { _method = value; };
		public function get method():String { return _method; };
		//远程地址
		private var _url:String = '';
		public function set url(value:String):void { _url = value; };
		public function get url():String { return _url; };
		//返回格式
		//private var _resultFormat:String = 'text';
		private var _resultFormat:String =URLLoaderDataFormat.TEXT
		public function set resultFormat(value:String):void { _resultFormat = value; };
		public function get resultFormat():String { return _resultFormat; }
		//返回处理函数
		private var _result:Function = null;
		public function set result(value:Function):void { _result = value; };
		public function get result():Function { return _result; };
		//出错处理函数
		private var _fault:Function = null;
		public function set fault(value:Function):void { _fault = value; };
		public function get fault():Function { return _fault; }
	}

}